package com.yourannet.android.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

import com.yourannet.utils.FileUtils;

public class PositionUpload extends Service {

	private boolean quit;
	private LocationManager loctionManager;
	private Location location;
	private Criteria criteria;
	private File file;
	private StringBuilder sb;
	private SimpleDateFormat df,dfd;
	private String dir;
	private String fileName;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
        loctionManager=(LocationManager) getSystemService(
        		Context.LOCATION_SERVICE); 
        String providerGPS=android.location.LocationManager.GPS_PROVIDER;
        //2秒记录一次，1米记录一次
        loctionManager.requestLocationUpdates(providerGPS, 2*1000, 1, new LocationListener() {
        			@Override
        			public void onStatusChanged(String provider, int status, Bundle extras) {
        			}
        			@Override
        			public void onProviderEnabled(String provider) {
        			}
        			@Override
        			public void onProviderDisabled(String provider) {
        			}
        			//当位置变化时触发
        			@Override
        			public void onLocationChanged(Location loc) {
        				location = loc;
        			}
        			});
		 location =  loctionManager.getLastKnownLocation(providerGPS);
        df = new SimpleDateFormat("HH:mm:ss",Locale.CHINA);
        dfd = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		this.quit = true; 
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		if(intent.getExtras()!=null){
			dir = intent.getExtras().getString("directory");
			fileName=dfd.format(new Date())+".txt";
			if(FileUtils.CreateFileInSDCard(dir, fileName)==0){
				//打开文件
                file = new File(dir,fileName);
                new Thread() { 
                    @Override 
                    public void run() { 
                        while (!quit) { 
                        	 try { 
                        		 Thread.sleep(2*1000); 
                        		 if(location!=null){
                        			 sb= new StringBuilder("\n");
                            		 sb.append( df.format(new Date()));
                                     String position = " "+location.getLatitude()+" , "+location.getLongitude();
                                     sb.append(position);
                                     FileUtils.WriteTxtFile(sb.toString(), dir+"/"+fileName);
                        		 }
                        		 
                             } catch (InterruptedException e) {
        						// TODO Auto-generated catch block
        						e.printStackTrace();
        					}
                        } 
                    } 
                }.start(); 
			}
		}
		return super.onStartCommand(intent, flags, startId); 

	}
}
